﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace davejk.Minecraft.ServerWrapper.Utils
{
    public class ConfigHelper
    {

        public static bool ConfigFileExists
        {
            get
            {
                return File.Exists(SetupHelper.AppBaseDirectory + "\\Minecraft\\server.properties");
            }
        }

        public static Dictionary<string, string> GetServerProperties()
        {
            TextReader ServerPropsReader = new StreamReader(SetupHelper.AppBaseDirectory + "\\Minecraft\\server.properties");
            string line;
            Dictionary<string, string> ServerProps = new Dictionary<string, string>();
            while ((line = ServerPropsReader.ReadLine()) != null)
            {
                if (line.StartsWith("#"))
                    continue;
                try
                {
                    string Key = line.Substring(0, line.IndexOf('='));
                    string Value = line.Substring(line.IndexOf('=') + 1);
                    ServerProps.Add(Key, Value);
                }
                catch { }
            }
            return ServerProps;
        }
        public static string GetNamedProperty(string Key)
        {
            Dictionary<string, string> ServerProps = GetServerProperties();
            if (ServerProps.ContainsKey(Key))
                return ServerProps[Key];
            else
                return String.Empty;
        }

        public static void SetServerProperties(Dictionary<string, string> NewSettings)
        {
            //TODO: Read through props file, then look up and change supplied settings and save
            throw new NotImplementedException();
        }
        public static void SetNamedProperty(string Key, string Value)
        {
            SetServerProperties(new Dictionary<string, string> { {Key, Value} });
        }

    }
}
